﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Medianamik.Core;
using System.Collections.Generic;
using Medianamik.Core.CodeGeneration;
using Medianamik.Core.Sugar;
using Telerik.Web.UI;

namespace Medianamik.UI.TypeManagement
{
    public partial class InterfacePropertyList : BasePage
    {
        #region Fields

        private Guid? _interfaceId;  //Param obligatoire

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            //Param obligatoire
            #region Param interfaceid

            if (!StringHelper.IsGuid(Request.QueryString["interfaceid"], ref _interfaceId)
                || CurrentInterface == null)
            {
                Response.Redirect(ReturnUrl);
            }

            #endregion

            if (!IsPostBack)
            {
                if (Implementers.Count() > 0 && CurrentInterface.EligibleProperties.Count() == 0)
                    PreventFromAddingProperties(true);

                LoadBreadcrumbs();

                AddPropertyLink.NavigateUrl = AddURL;
            }
        }

        private void PreventFromAddingProperties(bool doIt)
        {
            if (doIt)
            {
                PreventFromAddingPropertiesLabel.Visible = true;
                AddPropertyLink.Enabled = false;
                AddPropertyLink.CssClass = "Action BtAjouter-Inactif";
            }
            else
            {
                PreventFromAddingPropertiesLabel.Visible = false;
                AddPropertyLink.Enabled = true;
                AddPropertyLink.CssClass = "Action BtAjouter";
            }
        }

        protected void PropertyGridItemCommand(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeleteInterfaceProperty":
                    CurrentInterface.Properties.Remove(new Guid(e.CommandArgument.ToString()));
                    _currentInterface = NodeTypeManager.SaveInterface(CurrentInterface);

                    var code = new CodeService();
                    code.GenerateView(CurrentInterface);

                    PreventFromAddingProperties(false);

                    PropertyGrid.Rebind();
                    break;
                default:
                    break;
            }
        }

        protected void PropertyGridNeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            PropertyGrid.DataSource = CurrentInterface.Properties;
        }

        #endregion

        #region Properties

        private IEnumerable<NodeTypeInterfaceImplementation> _implementers;
        public IEnumerable<NodeTypeInterfaceImplementation> Implementers
        {
            get
            {
                if (_implementers == null)
                {
                    _implementers = NodeTypeManager.GetNodeTypes(CurrentInterface.ID);
                }

                return _implementers;
            }
        }

        private NodeTypeInterface _currentInterface;
        public NodeTypeInterface CurrentInterface
        {
            get
            {
                if (_currentInterface == null && _interfaceId.HasValue)
                    _currentInterface = NodeTypeManager.GetNodeTypeInterface(_interfaceId.Value);

                return _currentInterface;
            }
        }

        public const string ReturnUrl = "interfacelist.aspx"; 

        private string _addURL = null;
        public string AddURL
        {
            get
            {
                if (_addURL == null && CurrentInterface != null)
                    _addURL = "interfacepropertymanagement.aspx?interfaceid=" 
                        + CurrentInterface.ID;

                return _addURL;
            }
        }

        #endregion

        #region Methods

        protected void SetRowPropertyStyle(WebControl pControl, Guid pTypeID)
        {

        }

        private void LoadBreadcrumbs()
        {
            var breadcrumbs = new List<Breadcrumb>();
            breadcrumbs.Insert(0, new Breadcrumb(null,
                Resources("Medianamik_TypeManagement", "InterfaceManagement")));
            breadcrumbs.Add(new Breadcrumb(null,
                CurrentInterface.Name));
            BreadcrumbTrail.Breadcrumbs = breadcrumbs;
            BreadcrumbTrail.DataBind();
        }

        #endregion
    }
}